//
//  CHStringTool.swift
//  bridging_js
//
//  Created by cyptt on 2021/8/3.
//

import UIKit

class CHStringTool: NSObject {

    
    /**
        字典转换为JSONString
        
        - parameter dictionary: 字典参数
        
        - returns: JSONString
        */
    static func dicValueString(_ dic:[String : Any]) -> String?{
           let data = try? JSONSerialization.data(withJSONObject: dic, options: [])
           let str = String(data: data!, encoding: String.Encoding.utf8)
           return str
       }

    
    // MARK:-- 字符串转字典
    static func stringValueDic(_ str: String) -> [String : Any]?{
        let data = str.data(using: String.Encoding.utf8)
        if let dict = try? JSONSerialization.jsonObject(with: data!,
                        options: .mutableContainers) as? [String : Any] {
            return dict
        }

        return nil
    }
    
    
    //MARK:--
    
    /**
        image转base64
     */
    static func imageToBase64(image:UIImage) -> String {
        let data = image.pngData()
     
        let base64:String = data!.base64EncodedString()

        return base64
    }
    
    /**
     获取当前时间戳
     */
    static func getNowTimeInterval()->Int{
        let  now =  NSDate ()
        let  timeInterval: TimeInterval  = now.timeIntervalSince1970
        let  timeStamp =  Int (timeInterval)
        return timeStamp
    }
    
    
}
